package com.yserp.project.erp.order.productorder.controller;

import com.yserp.common.exception.BusinessException;
import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.order.productorder.domain.ProductionOrder;
import com.yserp.project.erp.order.productorder.domain.ProductionOrderDto;
import com.yserp.project.erp.order.productorder.domain.ProductionOrderVo;
import com.yserp.project.erp.order.productorder.service.IProductionOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 生产批次Controller
 *
 * @author yserp
 * @date 2022-07-19
 */
@Controller
@RequestMapping("/order/productorder")
public class ProductionOrderController extends BaseController {
    private String prefix = "erp/order/productorder";

    @Autowired
    private IProductionOrderService productionOrderService;

    @RequiresPermissions("order:productorder:view")
    @GetMapping()
    public String productorder() {
        return prefix + "/productorder";
    }

    /**
     * 查询生产批次列表
     */
    @RequiresPermissions("order:productorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductionOrder productionOrder) {
        startPage();
        List<ProductionOrder> list = productionOrderService.selectProductionOrderList(productionOrder);
        return getDataTable(list);
    }

    /**
     * 导出生产批次列表
     */
    @RequiresPermissions("order:productorder:export")
    @Log(title = "生产订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductionOrder productionOrder) {
        List<ProductionOrder> list = productionOrderService.selectProductionOrderList(productionOrder);
        ExcelUtil<ProductionOrder> util = new ExcelUtil<ProductionOrder>(ProductionOrder.class);
        return util.exportExcel(list, "排产订单数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.PRODUCTION_ORDER.getPref()));
    }

    /**
     * 新增生产批次
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("productionOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.PRODUCTION_ORDER.getPref()));
        return prefix + "/add";
    }

    /**
     * 销售一键生产
     * @param id 销售单子表主键ID
     * @param mmap 装数据的容器
     * @return
     */
    @GetMapping("/saleProduct")
    public String saleProduct(@RequestParam Long id, ModelMap mmap) {
        mmap.put("productionOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.PRODUCTION_ORDER.getPref()));
        //获取生产单信息
        mmap.put("productionOrderVo",productionOrderService.saleTransProduct(id));
        return prefix + "/oneClickProduct";
    }

    /**
     * 新增保存生产批次
     */
    @RequiresPermissions("order:productorder:add")
    @Log(title = "生产订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductionOrderDto productionOrderDto) {
        return toAjax(productionOrderService.insertProductionOrder(productionOrderDto));
    }

    /**
     * 修改生产批次
     */
    @RequiresPermissions("order:productorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductionOrderVo vo = productionOrderService.selectProductionOrderById(id);
        mmap.put("productionOrderVo", vo);
        return prefix + "/edit";
    }

    /**
     * 修改保存生产批次
     */
    @RequiresPermissions("order:productorder:edit")
    @Log(title = "生产订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductionOrderDto productionOrderDto) {
        return toAjax(productionOrderService.updateProductionOrder(productionOrderDto));
    }

    /**
     * 修改状态
     */
    @RequiresPermissions("order:productorder:edit")
    @Log(title = "生产订单,更新状态", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/status")
    @ResponseBody
    public AjaxResult editStatus(ProductionOrder productionOrder) {
        return toAjax(productionOrderService.updateProductionOrderStatus(productionOrder));
    }

    /**
     * 删除生产批次
     */
    @RequiresPermissions("order:productorder:remove")
    @Log(title = "生产订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productionOrderService.deleteProductionOrderByIds(ids));
    }

    /**
     * 审核销售订单
     */
    @RequiresPermissions("order:productorder:audit")
    @Log(title = "生产订单", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult aduit(String ids) {
        return toAjax(productionOrderService.auditProductionOrderByIds(ids));
    }


    /**
     * 选择排产订单
     */
    @GetMapping("/selectProductOrder")
    public String selectProductOrder() {
        return prefix + "/selectProductOrder";
    }

    /**
     * 查询排产订单管理列表(选择页面)
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(ProductionOrder productionOrder) {
        startPage();
        List<ProductionOrder> list = productionOrderService.selectProductionOrderList(productionOrder);
        return getDataTable(list);
    }

    /**
     * 查询排产订单管理列表
     */
    @PostMapping("/ajaxList")
    @ResponseBody
    public AjaxResult ajaxList(ProductionOrder productionOrder) {
        List<ProductionOrder> list = productionOrderService.selectProductionOrderList(productionOrder);
        return AjaxResult.success(list);
    }

    /**
     * 通过id，查询商品伤情数据
     */
    @PostMapping("/ajaxData")
    @ResponseBody
    public AjaxResult ajaxData(ProductionOrder productionOrder) {
        if(null == productionOrder.getId()){
            throw new BusinessException("参数为空");
        }
        ProductionOrder order = productionOrderService.getById(productionOrder.getId());
        return AjaxResult.success(order);
    }


}
